#include "detailview.h"
#include <QVBoxLayout>
#include <QJsonDocument>
#include <QJsonObject>

DetailView::DetailView(QWidget *parent)
    : QWidget(parent) {
    m_textEdit = new QTextEdit(this);
    m_textEdit->setReadOnly(true);
    m_textEdit->setFontFamily("Consolas");
    
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(m_textEdit);
    layout->setContentsMargins(0, 0, 0, 0);
}

void DetailView::showDetails(const QModelIndex &index) {
    if (!index.isValid()) {
        clear();
        return;
    }
    
    QString details;
    QStandardItemModel *model = qobject_cast<QStandardItemModel*>(const_cast<QAbstractItemModel*>(index.model()));
    if (!model) return;
    
    QStandardItem *item = model->itemFromIndex(index);
    if (!item) return;
    
    QString type = item->data(Qt::UserRole).toString();
    
    if (type == "header") {
        details = "ELF Header Information:\n\n";
        for (int i = 0; i < item->rowCount(); ++i) {
            details += item->child(i)->text() + "\n";
        }
    } else if (type == "program_item") {
        details = "Program Header Details:\n\n";
        for (int i = 0; i < item->rowCount(); ++i) {
            details += item->child(i)->text() + "\n";
        }
    } else if (type == "section_item") {
        details = "Section Header Details:\n\n";
        for (int i = 0; i < item->rowCount(); ++i) {
            details += item->child(i)->text() + "\n";
        }
    } else if (type == "symbol_item") {
        details = "Symbol Details:\n\n";
        for (int i = 0; i < item->rowCount(); ++i) {
            details += item->child(i)->text() + "\n";
        }
    } else if (type == "string_item") {
        details = "String Value:\n\n" + item->text();
    } else {
        details = "Select an item to view details";
    }
    
    m_textEdit->setPlainText(details);
}

void DetailView::clear() {
    m_textEdit->clear();
}
